<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>登录</title>
    <link rel="stylesheet" href="./css/bootstrap.css" />
    <link rel="stylesheet" href="./css/login.css" />
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1">
                <div class="panel panel-default login">
                    <div class="panel-body">
                        <h2 class="text-center">图书管理系统</h2>
                        <form id="form1">
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名" />
                            </div>
                            <div class="form-group">
                                <label for="password">密码</label>
                                <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" />
                            </div>
                            <button type="submit" class="btn btn-primary btn-block">
                  登录
                </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="./js/jquery.js"></script>
    <script>
        var usern = document.querySelector("#username");
        var passw = document.querySelector("#password");
        var btn = document.querySelector(".btn");
        btn.addEventListener("click", function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "http://localhost:5001/user/login",
                data: {
                    username: usern.value,
                    password: passw.value,
                },
                success: function(res) {
                    console.log(res);
                    if (res.code == 0) {
                        localStorage.setItem("token", res.data.token);
                        location.href = "index.html";
                    } else {
                        alert("账号或密码错误");
                    }
                },
            });
        });
    </script>
</body>

</html>